/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package descent;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import descent.Packet.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.*;
import org.newdawn.slick.tiled.TiledMap;
/**
 *
 * @author jimmy
 */
public class Descent extends BasicGame {
   // private TiledMap map;
    public Client client;
    public ArrayList<String> messages = new ArrayList<String>();
   
        public Descent() {
            super("Hello World");
        }
        
@Override
  public void init(GameContainer gc) throws SlickException
  {
      // map = new TiledMap("data/map1.tmx");
       gc.setTargetFrameRate(60);
       client = new Client();
       register();
       NetworkListener nl = new NetworkListener();
       nl.init(client,this);
       client.addListener(nl);
       client.start();
        try {
            client.connect(60000, "127.0.0.1", 7386);
            //this.messages.add("test");
        } catch (IOException ex) {
            Logger.getLogger(Descent.class.getName()).log(Level.SEVERE, null, ex);
            client.stop();
        }
  }
 
  @Override
  public void update(GameContainer gc, int delta) throws SlickException
  {
     if (this.messages.size() >=5){
         this.messages.remove(0);
     }
  }
 
  @Override
  public void render(GameContainer gc, Graphics g) throws SlickException
  {
     //g.drawString("woot test", 100, 100);
     
      //map.render(0,0);
       int x = 100;
       int y = 100;
        for(int i= 0; i < this.messages.size();i++){
            
            
            g.drawString((this.messages.get(i).toString()), x, y);
            y+= 15;
            
            //messages.remove(i);
        }
    
  }       
        
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)  throws SlickException {
        
      AppGameContainer app = new AppGameContainer(new Descent());
 
     app.setDisplayMode(800, 600, false);
     app.start();
    }
    
    private void register(){
        Kryo kryo = client.getKryo();
        kryo.register(PacketLoginRequest.class);
        kryo.register(PacketLoginAccepted.class);
        kryo.register(PacketMessage.class);
    }
    
    @Override
    public void keyPressed(int key, char c){
        if (key == Input.KEY_T){
            PacketMessage message = new PacketMessage();
            message.message = "testing out server";
            client.sendTCP(message);
            
        }
        
        if (key == Input.KEY_R){
            this.messages.removeAll(messages); 
        }
        if (key == Input.KEY_W){
            this.messages.add("I hit the W key"); 
        }
    }
    
   
}
